// pages/find/find.js
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length === 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}
var timer = null;
import {http} from "../../utils/index"

Page({

  /**
   * 页面的初始数据
   */
  data: {
      toggle:true,
      percent:0,
      banner:null,
      name:"纤夫的爱",
      author:"老班长",
      poster:"http://47.104.209.44/base/img/3.jpg",
      aSrc:"http://47.104.209.44/mp/Sugar.mp3",
      vSrc:"http://47.104.209.44/mp/brkw.mp4",
      danmuList:
        [{
          text: '第 1s wuhan2004- 666 ',
          color: '#ff0000',
          time: 1
        }, {
          text: '第 3s 2004-daydayup',
          color: '#ff00ff',
          time: 3
        }],
        text:666,
        flag:true
  },
  choosenewvideo(){
    const that = this
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: ['front', 'back'],
      success(res) {
        that.setData({
          vSrc: res.tempFilePath
        })
      }
    })
  },
  sendDanmu(){
    this.videoCtx.sendDanmu({
      text:this.data.text,
      color:getRandomColor()
    })
    this.setData({
      text:""
    })
  },
  audioPlay(){
    this.audioCtx.play();
  },
  audioPause(){
    this.audioCtx.pause();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let timer = setInterval(()=>{
      if(this.data.percent<50){
        this.setData({
          percent:++this.data.percent
        })
      }else{
        clearInterval(timer)
        timer = null;
        this.seconeCountDown()
      }
    },5)
  },

  seconeCountDown(){
    let timer2 = setInterval(()=>{
      if(this.data.percent<100){
        this.setData({
          percent:++this.data.percent
        })
      }else{
        clearInterval(timer2)
        timer2 = null;
        this.setData({
          toggle:false
        })
        this.getBanner();
      }
    },1)
  },
  getBanner(){
    http({
      url:"https://shiwenmz.top:3333/top/playlist?limit=6&order=new",
      data:{},
      method:"GET",
      success:res=>{
        console.log(res);
        this.setData({
          banner:res.data.playlists
        });
        wx.showToast({
          title: '请求成功',
        })
      }
    })
  },

  showWarning(){
    this.videoCtx.pause();  // 暂停播放
    wx.showModal({
      title: '流量警告',
      content: '你正在使用4G网络观看视频!', 
      cancelText:"暂停观看",
      cancelColor:"#515151",
      confirmText:"继续观看",
      confirmColor:"#F56C6C",
      success: (res)=> {
        if (res.confirm) {
          console.log('用户点击确定');
          wx.showToast({
            title:"土豪,请继续"
          });
          this.videoCtx.play();
          wx.setStorageSync('isPlay', true)

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.showToast({
            title:"土豪,请充钱"
          })
        }
      }
    })
  },

  videotimeupdate(){
    if(!wx.getStorageSync('isPlay')&&this.data.flag){
      console.log("wifi --- 4G")
      this.showWarning();
      this.setData({
        flag:false
      })
    }
  },

  videoplay(){
      if(!wx.getStorageSync('isPlay')){
          this.showWarning()
      }
  },

  drawCanvas(){
    // 使用 wx.createContext 获取绘图上下文 context
      var context = wx.createCanvasContext('firstCanvas')
      context.setStrokeStyle("#00ff00")
      context.setLineWidth(5)
      context.rect(0, 0, 200, 200)
      context.stroke()
      context.setStrokeStyle("#ff0000")
      context.setLineWidth(2)
      context.moveTo(160, 100)
      context.arc(100, 100, 60, 0, 2 * Math.PI, true)
      context.moveTo(140, 100)
      context.arc(100, 100, 40, 0, Math.PI, false)
      context.moveTo(85, 80)
      context.arc(80, 80, 5, 0, 2 * Math.PI, true)
      context.moveTo(125, 80)
      context.arc(120, 80, 5, 0, 2 * Math.PI, true)
      context.stroke()
      context.draw()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
      this.drawCanvas();
      // 使用 wx.createAudioContext 获取 audio 上下文 context
      this.audioCtx = wx.createAudioContext('myAudio')
      this.videoCtx = wx.createVideoContext('myVideo')

      wx.getNetworkType({
        success (res) {
          const networkType = res.networkType;
          wx.setStorageSync('isPlay', networkType==="wifi")
        }
      })

      wx.onNetworkStatusChange(function (res) {
        const networkType = res.networkType;
        wx.setStorageSync('isPlay', networkType==="wifi")
      })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})